package web;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Date;

@WebServlet("/ServletContextDemo2")
public class ServletContextDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建对象
        ServletContext context = request.getServletContext();
        //获取应用初始化的参数
        String configPath = context.getInitParameter("configPath");
        System.out.println(configPath);//classpath:config.properties
        //获取应用访问的虚拟路径
        String contextPath = context.getContextPath();
        System.out.println(contextPath);
        //根据虚拟目录获取应用中的绝对路径
        //1.src新建 a.txt
        String aPath = context.getRealPath("WEB-INF/classes/a.txt");
        System.out.println(aPath);
        //2.web/WEB-INF 新建 b.txt
        String bPath = context.getRealPath("WEB-INF/b.text");
        System.out.println(bPath);
        //3.web新建 c.txt
        String cPath = context.getRealPath("c.text");
        System.out.println(cPath);
        //4.在工程下新建d.txt - 读不到，文件不编译
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().write(new Date().toString());
    }
}
